package messages_erreur;

import java.util.Hashtable;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import managers.MessagesErreurManagerRemote;

import Base.MessagesErreur;

/**
 * Message-Driven Bean implementation class for: MDB_Erreur
 * 
 */
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }, mappedName = "MDB_Erreur")
@TransactionManagement(TransactionManagementType.BEAN)
public class MDB_Erreur implements MessageListener {

	/**
	 * Default constructor.
	 */
	public MDB_Erreur() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see MessageListener#onMessage(Message)
	 */
	public void onMessage(Message message) {
		try {
			if (!(message instanceof TextMessage)) {
				System.out.println("onMessage() received an unhandled: "
						+ message.getClass().getName());
				return;
			}
			process((TextMessage) message);
		} catch (Throwable t) {
			System.err
					.println((t instanceof JMSException) ? "JMSException"
							: "Throwable" + " Caught in onMessage(): "
									+ t.getMessage());
		}

	}

	private void process(TextMessage message) throws JMSException {
		Properties props = new Properties();
		props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.rmi.registry.RegistryContextFactory");
		props.setProperty("java.naming.factory.url.pkgs",
				"org.jonas.naming:org.jnp.interfaces");
		props.setProperty(Context.PROVIDER_URL, "127.0.0.1:16010");
		try {
			Context ctx = new InitialContext(props);
		
		
//		Hashtable<String, String> h = new Hashtable<String, String>();
//		h.put("java.naming.factory.initial",
//				"org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
//		h.put("java.naming.provider.url", "rmi://localhost:1099");
//		h.put("java.naming.factory.url.pkgs", "org.objectweb.jonas.naminsg");
		
		//Context ctx;

		String text;
		
		text = message.getText();

		MessagesErreur mess = new MessagesErreur();
		StringTokenizer st = new StringTokenizer(text, ":");

		mess.setCodeMessage(Integer.parseInt(st.nextToken()));
		mess.setCommentaireMessage(st.nextToken());

		
		try {
			//ctx = new InitialContext(h);
			String JNDI_NAME = "managers.MessagesErreurManager" + "_"
					+ MessagesErreurManagerRemote.class.getName() + "@Remote";
			MessagesErreurManagerRemote ref = (MessagesErreurManagerRemote) ctx.lookup(JNDI_NAME);

			
			ref.creerMessagesErreur(mess);

			message.acknowledge();
			
		} catch (NamingException e) {
			e.printStackTrace();
			System.out.println("Erreur : ajout du message d'erreur dans la base de données impossible "+e.getMessage());
		}
		} catch (NamingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

}
